/**
 * @author Kristian Mandrup
 * @date May 14, 2009
 * @version 0.8
 * 
 * m3gen:: Example execution 
 * 
 * Using a person ruleSet and person json model 
 * - ruleSets/m3gen.ruleSet.person.js
 * - model/simplePersonModel.js
 * 
 */

// for JSON schema (not supported in v.0.1)
dojo.require("dojox.json.schema");
// for resolving JSON references 
dojo.require("dojox.json.ref");

// override selectTitle function with simple selector object
var ruleSets = ruleSetConfig_person.ruleSets;
ruleSets.person.functions.selectTitle = {seperator: ' ', selectors: ['name.first', 'name.last']};

var resolvedModel = personsModel;

// output the resolved model
var jsonFullModelStr = dojo.toJson(resolvedModel);
m3gen.log('json Full Model:' + jsonFullModelStr);

// openList defines the ids of objects that are to be showed in open state
var my_openTreeNodeList = ['company_Microsoft'];

// create model used to parse the instance model and generate the resulting jsTree model 
var generatorModel = { 
	name: 'parsePersonModel', 
	ruleSetConfiguration: ruleSetConfig_person,
	openList: my_openTreeNodeList
}

/*
 * Optionally set custom constructors for event & exception handling
 * 
 */
/*
var constructors = {
	eventController:	
	exceptionHandler:
}
*/	

// create the full model
var fullModel_Person = {
    /* jsonInstanceModel: resolvedModel, */ /* Optionally set instance model here */
	generatorModel: generatorModel
	/* ,constructors: constructors */ /*Optionally set custom constructors */
};

// jsTree.plugins.generator.eventController.settings = jsTree.plugins.generator.defaults.eventSettings.warnOnly; 

// generate the jsTree model
// TODO: allow generatorModel as direct parameter to use default settings
var m3generator = new m3gen.JsTreeModelGenerator(fullModel_Person);

var executeOptions = {
	languagesOn: false,
	includeEmptyBranchesOn: false,
	jsonInstanceModel: resolvedModel
};

// if instance model has been set during creation of m3generator, only pass model in execute to override 'default' model
// TODO: Allow options on execution
// var treeData = m3generator.execute(resolvedModel);
var treeData = m3generator.execute(executeOptions);

// display the resulting model
var jsonStrGen = dojo.toJson(treeData);
m3gen.log('json generated jsTree:' + jsonStrGen);

$(document).ready(function(){
    $('#model').text(jsonStrGen);
	var eventLog = $('ul#eventList');
	var eventList = m3gen.eventList;
	for each (event in eventList) {
		eventLog.append('<li>' + event + '</li>');
	}

});
